package com.huoniu.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huoniu.api.entity.Patient;
import com.huoniu.api.entity.request.PatientParam;
import com.huoniu.api.mapper.PatientMapper;
import com.huoniu.common.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @类 名： PatientController <br/>
 * @描 述： <br/>
 * @日 期： 2020/9/8 15:14<br/>
 * @作 者： 侯建军<br/>
 * @版 本： 1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private PatientMapper patientMapper;

    /**
     * 添加患者
     *
     * @param Patient
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody Patient Patient) {
        if (patientMapper.insert(Patient) > 0) {
            return Result.success("患者添加成功");
        }
        return Result.fail("患者添加失败");
    }

    @GetMapping
    public Result getPatient(PatientParam patientParam) {
        // 分页
        Page page=new Page();
        page.setCurrent(patientParam.getCurrentPage());
        page.setSize(patientParam.getPageSize());
        // 条件查询
        QueryWrapper queryWrapper = new QueryWrapper();
        // 进行降序排列
        queryWrapper.orderByDesc("create_date");
        IPage<Patient> iPage = patientMapper.selectPage(page,queryWrapper);
        return Result.success(iPage);
    }

    /**
     * 按ID查询
     * @param cardId
     * @return
     */
    @GetMapping("/{cardId}")
    public Result getPatientByCardId(@PathVariable("cardId") String cardId) {
        // 条件查询
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("card_id",cardId);
        Patient patient = patientMapper.selectOne(queryWrapper);
        return Result.success(patient);
    }
}
